package com.j283_5.wisdom_park.controller.admin_login;

import com.j283_5.wisdom_park.common.result.ResultData;

import com.j283_5.wisdom_park.service.LoginService;
import com.j283_5.wisdom_park.service.impl.LoginServiceImpl;
import com.j283_5.wisdom_park.vo.login_information.LoginInformationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


/**
 * @author shkstart
 * @create 2022-05-10-11:04
 */
@Transactional
@RestController
@RequestMapping("/index")
@Api(value = "用户登录",tags = "用户登录",produces="王银伟")
public class AdminLoginController {


    @Resource
    private LoginService loginService;

    @RequestMapping(value = "/login" , method = RequestMethod.POST)
    @ApiOperation(value = "登录",notes = "登录",produces="王银伟", response = ResultData.class)
    public ResultData login(String username,String password){
//        //获取当前的用户
//        Subject subject = SecurityUtils.getSubject();
//        //封装用户的登录数据
//        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
//        //token
////        String token = null;
////        String token = JWT.create()
////                .withHeader(map) //Header 请求头参数，可以不填写
//        try {
//            subject.login(usernamePasswordToken);//执行登录的方法，如果没有异常说明ok
////            Map<String, Object> map = new HashMap<>();
////            Calendar instance = Calendar.getInstance();
////            instance.add(Calendar.SECOND, 600);
////            token = JWT.create()
////                    .withHeader(map) //Header 请求头参数，可以不填写
////                    .withClaim("phone", password) //Payload 有效载荷 可以填写多个，不敏感信息
////                    .withExpiresAt(instance.getTime()) //设置过期时间
////                    .sign(Algorithm.HMAC256("SECRET"));//签名
//        }catch (UnknownAccountException c){//用户名不存在
//             return ResultData.ok().data("用户名不存在");
//        }
//        if (!loginService.queryLoginInformation(username,password)){
//            return ResultData.error().message("登录错误");
//        }

        if (loginService.queryLoginInformation(username,password)){
            LoginInformationVo loginInformationVo = loginService.selectLoginInformation(username);

            Map map = new HashMap();
            map.put("id",loginInformationVo.getAdminRoleId());
            map.put("name",loginInformationVo.getLogUserName());
//            map.put("ip","186.129.123.168");
            return ResultData.ok().data(map);
        }

        return ResultData.error().message("登录失败");
    }

    @RequestMapping(value = "/quit" , method = RequestMethod.POST)
    @ApiOperation(value = "退出",notes = "退出",produces="王银伟", response = ResultData.class)
//    @RequiresPermissions("SuperAdmin")
    public ResultData quit(){

        return ResultData.ok().data("退出登录成功");
    }
}
